
package com.hlkj.pay.controller.openapi;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.LocalContext;
import com.hlkj.framework.common.pojo.PageParam;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.user.IUserAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.LocalUserRequest;
import com.hlkj.pay.dto.user.UserTeleChargeOrderQueryDto;
import com.hlkj.pay.infrastructure.model.user.UserCouponDO;
import com.hlkj.pay.infrastructure.model.user.UserCouponInfoDO;
import com.hlkj.pay.infrastructure.model.user.UserTeleChargeOrderDO;
import com.hlkj.pay.service.user.IUserCouponService;
import com.hlkj.pay.service.user.IUserRechargeService;
import com.hlkj.pay.vo.common.resp.token.OauthTokenResp;
import com.hlkj.pay.vo.user.req.UseChargeReq;
import com.hlkj.pay.vo.user.req.UseLoginReq;
import com.hlkj.pay.vo.user.req.UserTeleChargeOrderQueryPageReq;
import com.hlkj.pay.vo.user.resp.UserCouponInfoResp;
import com.hlkj.pay.vo.user.resp.UserTeleChargeOrderResp;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/25 14:16
 */
@Tag(name = "广告用户相关接口")
@RestController
@RequestMapping("/openapi/user")
@Slf4j
@RequiredArgsConstructor
public class OpenApiUserController extends BaseController {

    private final IUserAppService userAppService;

    private final IUserCouponService userCouponService;

    private final IUserRechargeService userRechargeService;

    @Operation(summary = "发送短信验证码")
    @GetMapping("/sendAuthCode")
    @ResponseBody
    CommonResult<Void> sendAuthCode(@RequestParam("phone") String phone) {
        return userAppService.sendAuthCode(phone);
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    @ResponseBody
    CommonResult<OauthTokenResp> login(@Validated @RequestBody UseLoginReq useLoginReq) {
        return userAppService.login(useLoginReq);
    }

    @Operation(summary = "优惠劵列表")
    @GetMapping("/coupon")
    @ResponseBody
    CommonResult<List<UserCouponInfoResp>> couponList(@RequestParam(value = "status",required = false) Integer status,PageParam pageParam) {
        LocalUserRequest localUserRequest = (LocalUserRequest)LocalContext.get();
        Long userId = localUserRequest.getUserId();
        PageResult<UserCouponDO> page = userCouponService.userCouponList(userId,status, pageParam);
        List<UserCouponDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        Set<Long> couponInfoIds = list.stream().map(userCouponDO -> userCouponDO.getCouponId()).collect(Collectors.toSet());
        List<UserCouponInfoDO> userCouponInfoList = userCouponService.couponInfoList(couponInfoIds);
        Map<Long, UserCouponInfoDO> userCouponInfoMap = userCouponInfoList.stream()
                .collect(Collectors.toMap(userCouponInfoDO -> userCouponInfoDO.getId(), Function.identity()));
        List<UserCouponInfoResp> userCouponInfoResps = BeanUtil.copyToList(list, UserCouponInfoResp.class);
        userCouponInfoResps.stream().forEach(userCouponInfoResp -> {
            UserCouponInfoDO userCouponInfoDO = userCouponInfoMap.get(userCouponInfoResp.getCouponId());
            if(userCouponInfoDO!=null){
                userCouponInfoResp.setName(userCouponInfoDO.getName());
                userCouponInfoResp.setDescription(userCouponInfoDO.getDescription());
                userCouponInfoResp.setType(userCouponInfoDO.getType());
                userCouponInfoResp.setValue(userCouponInfoDO.getValue());
                userCouponInfoResp.setMinAmount(userCouponInfoDO.getMinAmount());
            }
        });
        return buildPageResult(page, userCouponInfoResps);
    }

    @Operation(summary = "话费充值")
    @PostMapping("/charge")
    @ResponseBody
    CommonResult<Void> charge(@Validated @RequestBody UseChargeReq useChargeReq) {
        LocalUserRequest localUserRequest = (LocalUserRequest)LocalContext.get();
        Long userId = localUserRequest.getUserId();
        return userAppService.charge(userId, useChargeReq);
    }

    @Operation(summary = "充值列表")
    @GetMapping("/charge")
    @ResponseBody
    CommonResult<List<UserTeleChargeOrderResp>> couponList(UserTeleChargeOrderQueryPageReq userTeleChargeOrderQueryPageReq) {
        LocalUserRequest localUserRequest = (LocalUserRequest)LocalContext.get();
        Long userId = localUserRequest.getUserId();
        UserTeleChargeOrderQueryDto userTeleChargeOrderQueryDto = BeanUtil.copyProperties(userTeleChargeOrderQueryPageReq, UserTeleChargeOrderQueryDto.class);
        userTeleChargeOrderQueryDto.setUserId(userId);
        PageResult<UserTeleChargeOrderDO> page = userRechargeService.queryPage(userTeleChargeOrderQueryDto);
        List<UserTeleChargeOrderDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        return buildPageResult(page, BeanUtil.copyToList(list, UserTeleChargeOrderResp.class));
    }
}
